package com.bdqn.shequ.modules.sys.controller;

import com.bdqn.shequ.common.annotation.SysLog;
import com.bdqn.shequ.common.utils.PageUtils;
import com.bdqn.shequ.common.utils.R;
import com.bdqn.shequ.common.validator.ValidatorUtils;
import com.bdqn.shequ.common.validator.group.AddGroup;
import com.bdqn.shequ.common.validator.group.UpdateGroup;
import com.bdqn.shequ.modules.sys.entity.SysUserEntity;
import com.bdqn.shequ.modules.sys.entity.TcActivityEntity;
import com.bdqn.shequ.modules.sys.entity.TcMessageEntity;
import com.bdqn.shequ.modules.sys.service.TcActivityService;
import com.bdqn.shequ.modules.sys.service.TcMessageService;
import com.bdqn.shequ.modules.sys.utils.test;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 活动表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-04-11 16:18:25
 */
@RestController
@RequestMapping("sys/tcactivity")
public class TcActivityController {
    @Autowired
    private TcActivityService tcActivityService;

    @Autowired
    private TcMessageService tcMessageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:tcactivity:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tcActivityService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:tcactivity:info")
    public R info(@PathVariable("id") Long id){
        TcActivityEntity tcActivity = tcActivityService.getById(id);

        return R.ok().put("tcActivity", tcActivity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:tcactivity:save")
    public R save(@RequestBody TcActivityEntity tcActivity,HttpSession session){
        ValidatorUtils.validateEntity(tcActivity, AddGroup.class);
        tcActivity.setCreateTime(new Date());
        tcActivity.setActivityStatus("APPLY_UN_AUDIT");

        /*String appId = (String) */SysUserEntity user=(SysUserEntity)session.getAttribute("loginuser");
        String appId= user.getUsername();
        tcActivity.setApplicantId(appId);
        tcActivityService.save(tcActivity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:tcactivity:update")
    public R update(@RequestBody TcActivityEntity tcActivity){
        tcActivity.setUpdateTime(new Date());
        ValidatorUtils.validateEntity(tcActivity, UpdateGroup.class);
        tcActivityService.updateById(tcActivity);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:tcactivity:delete")
    public R delete(@RequestBody Long[] ids){
        tcActivityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 审核
     */
    @SysLog("审核")
    @RequestMapping("/shenhe")
    public R shenhe(String id, String status,String phone,String cname,String smstext,String address,String concatId,HttpSession session){
        System.out.println(""+phone);

        test test1 = new test();
        boolean isRight=test1.xinxi(smstext,phone);

        if(isRight){
            boolean flag = tcActivityService.updateStatus(Long.parseLong(id),status);
            if(!flag){
                return R.error("审核失败");
            }


            SysUserEntity user=(SysUserEntity)session.getAttribute("loginuser");
            TcMessageEntity tcMessage = new TcMessageEntity();
            tcMessage.setMessageReceivertelephone(phone);
            tcMessage.setMessageSituation("MEETING_AUDIT");
            tcMessage.setNoticeContent(smstext);
            tcMessage.setCreateTime(new Date());
            tcMessage.setUpdateUser(user.getUserId());
            tcMessage.setCreateUser(user.getUserId());

            tcMessageService.save(tcMessage);
            /*if(status.equals("ACTIVITY_AUDIT_PASS")){
                Snippet sn = new Snippet();
                sn.fillTemplate(concatId,phone,user.getUsername(),cname,address,"活动审核通过");
            }*/
            return R.ok();
        }else{
            return R.error("短信条数不足，无法发送！");
        }

    }


    /**
     * 执行
     */
    @SysLog("执行")
    @RequestMapping("/option")
    public R option(String id, String status,String phone,String smstext,HttpSession session){

        boolean flag = tcActivityService.updateStatus(Long.parseLong(id),status);
        if(!flag){
            return R.error("执行失败");
        }
        SysUserEntity user=(SysUserEntity)session.getAttribute("loginuser");
        TcMessageEntity tcMessage = new TcMessageEntity();
        tcMessage.setMessageReceivertelephone(phone);
        tcMessage.setMessageSituation("MEETING_AUDIT_OPTION");
        tcMessage.setNoticeContent(smstext);
        tcMessage.setCreateTime(new Date());
        tcMessage.setUpdateUser(user.getUserId());
        tcMessage.setCreateUser(user.getUserId());

        tcMessageService.save(tcMessage);
        return R.ok();
    }


}
